package cn.ctodb.mall.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.springframework.data.elasticsearch.annotations.Document;

import javax.persistence.*;
import java.io.Serializable;
import java.time.ZonedDateTime;
import java.util.HashSet;
import java.util.Set;
import java.util.Objects;

/**
 * A DeliveryOrder.
 */
@Entity
@Table(name = "delivery_order")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "deliveryorder")
public class DeliveryOrder implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "order_sn")
    private String orderSn;

    @Column(name = "create_date")
    private ZonedDateTime createDate;

    @OneToMany(mappedBy = "deliveryOrder")
    @JsonIgnore
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<DeliveryGoods> goods = new HashSet<>();

    @ManyToOne
    private Member member;

    @ManyToOne
    private Shop shop;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getOrderSn() {
        return orderSn;
    }

    public DeliveryOrder orderSn(String orderSn) {
        this.orderSn = orderSn;
        return this;
    }

    public void setOrderSn(String orderSn) {
        this.orderSn = orderSn;
    }

    public ZonedDateTime getCreateDate() {
        return createDate;
    }

    public DeliveryOrder createDate(ZonedDateTime createDate) {
        this.createDate = createDate;
        return this;
    }

    public void setCreateDate(ZonedDateTime createDate) {
        this.createDate = createDate;
    }

    public Set<DeliveryGoods> getGoods() {
        return goods;
    }

    public DeliveryOrder goods(Set<DeliveryGoods> deliveryGoods) {
        this.goods = deliveryGoods;
        return this;
    }

    public DeliveryOrder addGoods(DeliveryGoods deliveryGoods) {
        goods.add(deliveryGoods);
        deliveryGoods.setDeliveryOrder(this);
        return this;
    }

    public DeliveryOrder removeGoods(DeliveryGoods deliveryGoods) {
        goods.remove(deliveryGoods);
        deliveryGoods.setDeliveryOrder(null);
        return this;
    }

    public void setGoods(Set<DeliveryGoods> deliveryGoods) {
        this.goods = deliveryGoods;
    }

    public Member getMember() {
        return member;
    }

    public DeliveryOrder member(Member member) {
        this.member = member;
        return this;
    }

    public void setMember(Member member) {
        this.member = member;
    }

    public Shop getShop() {
        return shop;
    }

    public DeliveryOrder shop(Shop shop) {
        this.shop = shop;
        return this;
    }

    public void setShop(Shop shop) {
        this.shop = shop;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        DeliveryOrder deliveryOrder = (DeliveryOrder) o;
        if (deliveryOrder.id == null || id == null) {
            return false;
        }
        return Objects.equals(id, deliveryOrder.id);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(id);
    }

    @Override
    public String toString() {
        return "DeliveryOrder{" +
            "id=" + id +
            ", orderSn='" + orderSn + "'" +
            ", createDate='" + createDate + "'" +
            '}';
    }
}
